package com.example.springbootdemo.异步注解失效测试.shedule;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Author forsake
 * @Description  两个定时任务同时执行
 * @Date 17:08 2021/8/30
 **/

/**
 * 当前类不交给spring托管，定时任务失效
 * 两个定时任务同一个时间点，谁先抢到锁谁先执行
 */
@Component
public class ScheduleOne {
    @Scheduled(cron = "0 36 20 * * ? ")
    public void test1() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程名："+Thread.currentThread().getName());
            System.out.println(String.format("第%d次执行任务一",i));
        }
    }
    @Scheduled(cron = "0 36 20 * * ? ")
    public void test2() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程名："+Thread.currentThread().getName());
            System.out.println(String.format("第%d次执行任务二",i));
        }
    }
}
